package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.Content;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.service.ContentService;
import com.jyy.prefabricated.utils.OrderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 内容表(Content)表控制层
 *
 * @author makejava
 * @since 2021-05-13 14:43:01
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class ContentController {
    /**
     * 服务对象
     */
    @Resource
    private ContentService contentService;

    /**
     * 新增内容
     *
     * @param content
     * @return
     */
    @PostMapping("/content")
    public AjaxResult insert(@Valid @RequestBody Content content) {
        Content service = contentService.insert(content);
        Content result = contentService.queryById(service.getId());
        return AjaxResult.success(result);
    }

    /**
     * 更新内容
     *
     * @param content
     * @return
     */
    @PutMapping("/content")
    public AjaxResult update(@Valid @RequestBody Content content) {

        contentService.update(content);
        Content result = contentService.queryById(content.getId());
        return AjaxResult.success(result);
    }

    /**
     * 删除内容
     *
     * @param dto
     * @return
     */
    @DeleteMapping("/content-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto) {
        if (dto.getIds().size() == 0) {
            throw new CustomException("参数id不能为空");
        }
        contentService.batchUpdateDeleted(dto);
        return AjaxResult.success();
    }


    /**
     * 查询内容详情
     *
     * @param id
     * @return
     */
    @GetMapping("/content/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find course by id:{}", id);
        Content result = contentService.queryById(id);
        return AjaxResult.success(result);
    }

    /**
     * 后台分页查询内容列表
     *
     * @param contentName       名称
     * @param deleted           是否删除
     * @param contentCategoryId 分类id
     * @param studentServiceId  学员服务id
     * @param pageable          分页信息
     * @param ascColumn  课程升序排序字段
     * @param descColumn   课程降序排序字段
     * @return
     */
    @GetMapping("/content")
    public AjaxResult selectByParams(@RequestParam(required = false) String contentName, @RequestParam(required = false) Integer contentCategoryId, @RequestParam(required = false) Integer studentServiceId, @RequestParam(required = false, defaultValue = "0") Integer deleted, @RequestParam(required = false, defaultValue = "") String ascColumn, @RequestParam(required = false, defaultValue = "") String descColumn, Pageable pageable) {
        String orderStr = OrderUtil.genOrderStr(ascColumn, descColumn);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Content> contentListontent = contentService.queryList(contentName, contentCategoryId, studentServiceId, deleted ,orderStr);
        PageInfo<Content> result = new PageInfo<>(contentListontent);
        return AjaxResult.success(result);
    }

    /**
     * 手机端查询内容列表
     *
     * @param contentName       名称
     * @param deleted           是否删除
     * @param contentCategoryId 分类id
     * @param studentServiceId  学员服务id
     * @param ascColumn  课程升序排序字段
     * @param descColumn   课程降序排序字段
     * @return
     */
    @GetMapping("/content-list")
    public AjaxResult selectByParams(@RequestParam(required = false) String contentName, @RequestParam(required = false) Integer contentCategoryId, @RequestParam(required = false) Integer studentServiceId, @RequestParam(required = false, defaultValue = "0") Integer deleted, @RequestParam(required = false, defaultValue = "") String ascColumn, @RequestParam(required = false, defaultValue = "") String descColumn) {
        String orderStr = OrderUtil.genOrderStr(ascColumn, descColumn);
        List<Content> result = contentService.queryList(contentName, contentCategoryId, studentServiceId, deleted ,orderStr);
        return AjaxResult.success(result);
    }


}
